#include "tcpsocketimpl.h"

CTcpSocketImpl::CTcpSocketImpl()
{
	m_socketFd	=	-1;
}
CTcpSocketImpl::~CTcpSocketImpl()
{
    
}
//////////////////////////////////////////////////////////////////////////
std_method_impl CTcpSocketImpl::ReadIo(IIoOperation* pOperation)
{
	HRESULT		hr = S_OK;

	return hr;
}
std_method_impl CTcpSocketImpl::WriteIo(IIoOperation* pOperation)
{
	HRESULT		hr = S_OK;

	return hr;
}
std_method_impl CTcpSocketImpl::ReadAllIo(IIoOperation* pOperation)
{
	HRESULT		hr = S_OK;

	return hr;
}
std_method_impl CTcpSocketImpl::WriteAllIo(IIoOperation* pOperation)
{
	HRESULT		hr = S_OK;

	return hr;
}
std_method_impl CTcpSocketImpl::ConnectIo(IIoOperation* pOperation)
{
	HRESULT		hr = S_OK;

	retrtpv(pOperation->GetAddress(), E_FAIL)
	retrtpv(pOperation->GetConPort(), E_FAIL)

	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(pOperation->GetAddress()))) {
		return E_FAIL;
	}

	unsigned short uport = pOperation->GetConPort()& UINT16_MAX;

	sin.sin_port = htons(uport);

	if (connect(m_socketFd, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
		return E_FAIL;
	}

	return hr;
}
std_method_impl CTcpSocketImpl::AcceptConnect(IIoOperation* pOperation)
{
	HRESULT		hr = S_OK;

	return hr;
}
std_method_impl CTcpSocketImpl::CloseIo()
{
	HRESULT		hr = S_OK;

#if (TARGET_OS == OS_WINDOWS)
	closesocket(m_socketFd);
#elif (TARGET_OS == OS_POSIX)
	close(m_socketFd);
#endif



	return hr;
}
std_method_impl CTcpSocketImpl::OpenIo()
{
	HRESULT		hr	=	S_OK;

    m_socketFd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (m_socketFd == INVALID_SOCKET) {
        return E_FAIL;
    }
	return hr;
}
std_method_impl CTcpSocketImpl::BindIo()
{
	return S_OK;
}
std_method_type_impl(os_sock_t) CTcpSocketImpl::GetSocket()
{
	return m_socketFd;
}
std_method_impl CTcpSocketImpl::BindSocket(os_sock_t sock)
{

	if (m_socketFd != INVALID_SOCKET)
	{

#if (TARGET_OS == OS_WINDOWS)
		closesocket(m_socketFd);
#elif (TARGET_OS == OS_POSIX)
		close(m_socketFd);
#endif
	}
	m_socketFd = sock;

	return S_OK;
}
std_method_impl CTcpSocketImpl::SetReuseAddr(bool bstart)
{
	HRESULT		hr	=	S_OK;
	
	return hr;
}
std_method_impl CTcpSocketImpl::SetNoDelay(bool bstart)
{
	HRESULT		hr	=	S_OK;

	return hr;
}
std_method_impl CTcpSocketImpl::SetLinger(bool bstart)
{
	HRESULT		hr	=	S_OK;

	return hr;
}
std_method_impl CTcpSocketImpl::SetKeepalive(bool bstart)
{
	HRESULT		hr	=	S_OK;

	return hr;
}
std_method_impl CTcpSocketImpl::SetCork(bool bstart)
{
	HRESULT		hr	=	S_OK;
	int set = 0;
	set = (bstart == true? 1 : 0);

#if (TARGET_OS != OS_WINDOWS)
	setsockopt(m_socketFd, SOL_TCP, TCP_CORK, (void *)&set, sizeof(int));
#endif

	return hr;
}
std_method_impl CTcpSocketImpl::SetNoSigPipe(bool bstart)
{

	HRESULT		hr	=	S_OK;

	int set = 0;
	set = (bstart == true? 1 : 0);

	//unix no found MSG_NOSIGNAL, but have SO_NOSIGPIPE
	
#if (TARGET_OS != OS_WINDOWS)

#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
	#define MSG_NOSIGNAL SO_NOSIGPIPE
#endif

	setsockopt(m_socketFd, SOL_SOCKET, MSG_NOSIGNAL, (void *)&set, sizeof(int)); 

#endif

	return hr;
}
std_method_impl CTcpSocketImpl::SetNonblock(bool bstart)
{
	return S_OK;
}
std_method_impl CTcpSocketImpl::SendBufLen(ULONG size)
{
	int nSendBuf = size * 1024;
	setsockopt(m_socketFd, SOL_SOCKET, SO_SNDBUF, (const char*)&nSendBuf, sizeof(int));

	return S_OK;
}
std_method_impl CTcpSocketImpl::RecvBufLen(ULONG size)
{
	int nRecvBuf = size * 1024;
	setsockopt(m_socketFd, SOL_SOCKET, SO_RCVBUF, (const char*)&nRecvBuf, sizeof(int));
	return S_OK;
}
std_method_impl CTcpSocketImpl::SendTimeOut(int sec)
{
	return S_OK;
}
std_method_impl CTcpSocketImpl::RecvTimeOut(int sec)
{
	return S_OK;
}
std_method_impl CTcpSocketImpl::ConnectTimeOut(int sec)
{
	return S_OK;
}
std_method_impl CTcpSocketImpl::SendLoWat(int size)
{
	return S_OK;
}
std_method_impl CTcpSocketImpl::RecvLoWat(int size)
{
	return S_OK;
}





